package com.iqiyi.pps.epg.core.service.sginal;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.sginal.TSginalBase;
import com.iqiyi.pps.epg.api.model.web.sginal.TSginalDataPage;
import com.iqiyi.pps.epg.api.model.web.sginal.TSginalServer;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.sginal.SginalBaseDataDao;
import com.iqiyi.pps.epg.core.model.sginal.SginalBase;
import com.iqiyi.pps.epg.core.model.sginal.SginalServer;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhang on 2017/4/27.
 */
@Service
@Transactional
public class SginalServiceImpl implements SginalService {

    private Logger logger = LoggerFactory.getLogger(SginalServiceImpl.class);

    @Autowired
    private SginalBaseDataDao dao;

    @Autowired
    private SginalServerService sginalServerService;

    @Override
    public SginalBase get(int id) {
        return dao.get(id);
    }

    @Override
    public int add(SginalBase entity) {
        dao.saveOnly(entity);
        return entity.getId();
    }

    @Override
    public void update(SginalBase entity) {
        dao.save(entity);
    }

    @Override
    public boolean checkByName(String name, Integer id) {
        Criteria criteria = dao.createCriteria();
        criteria.add(Restrictions.eq("name", name));
        criteria.add(Restrictions.ne("isDelete", SginalBase.IS_DELETE));
        if (id != null) {
            criteria.add(Restrictions.ne("id", id));
        }
        List<SginalBase> sginalBases = dao.find(criteria);
        return sginalBases!= null && sginalBases.size() > 0;
    }

    @Override
    public boolean checkByPort(String port, Integer id) {
        Criteria criteria = dao.createCriteria();
        criteria.add(Restrictions.eq("port", port));
        criteria.add(Restrictions.ne("isDelete", SginalBase.IS_DELETE));
        if (id != null) {
            criteria.add(Restrictions.ne("id", id));
        }
        List<SginalBase> sginalBases = dao.find(criteria);
        return sginalBases!= null && sginalBases.size() > 0;
    }

    @Override
    public TSginalDataPage getSginalData(TPage page, TFilter filter) {
        Map<String, String> map = filter.getFilters();
        String tempPattern = "${PARAMS}";
        String totalPattern = "select COUNT(*)";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        String hql = tempPattern + " from SginalBase";
        Page<SginalBase> dbPage = TransUtil.buildPage(page);

        hql = TransUtil.buildQueryStringByPropertyFilter(dbPage, hql, filterList);

        if (dbPage.isAutoCount()) {
            String totalCount = dao.getSession().createQuery(hql.replace(tempPattern, totalPattern)).uniqueResult().toString();
            page.setTotalCount(Long.valueOf(totalCount));
        }

        List result = dao.getSession().createQuery(hql.replace(tempPattern, ""))
                .setMaxResults(dbPage.getPageSize())
                .setFirstResult((dbPage.getPageNo() - 1) * dbPage.getPageSize())
                .list();
        TSginalDataPage tSginalDataPage = new TSginalDataPage();
        tSginalDataPage.setTPage(page);
        List<TSginalBase> tSginalDatas = new ArrayList<TSginalBase>();
        for(Object obj : result) {
            SginalBase sginalBase = (SginalBase) obj;
            List<SginalServer> sginalServers = sginalServerService.find(sginalBase.getId());
            List<TSginalServer> tSginalServers = new ArrayList<>();
            for(SginalServer sginalServer : sginalServers) {
                tSginalServers.add(sginalServer.convertThriftModel());
            }
            tSginalDatas.add(sginalBase.convertThriftModel(tSginalServers));
        }
        tSginalDataPage.setResult(tSginalDatas);
        return tSginalDataPage;
    }


}
